home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / utils / whati~gz.zoo / whatisin.c < prev    next >
C/C++ Source or Header  |  1992-09-13  |  7KB  |  360 lines

  1. #undef DEBUG
  2. #undef CHECK_MAGIC
  3.  
  4. /*
  5.  *    whatisin - show list of documentation sections
  6.  * 
  7.  *     whatisin [-P path] [section]
  8.  *
  9.  *    reads both $MANPATH/whatisin and $MANPATH/whatis._?_ files
  10.  *
  11.  *    bugs:
  12.  *        - need more error checking (of data in database)
  13.  *        - perhaps a "magic number" for the whatis database? it is
  14.  *          written, but #ifdef CHECK_MAGIC.
  15.  *
  16.  *    todo:
  17.  *        - invoke PAGER (or MANPAGER) on list if sections specified
  18.  *        - allow continuation lines in whatis database:
  19.  *
  20.  *            name\
  21.  *            %1\
  22.  *            %...
  23.  */
  24.  
  25. static char *rcsid_whatisin_c = "$Id: whatisin.c,v 2.0 1992/09/13 05:02:44 rosenkra Exp $";
  26. /*
  27.  * $Log: whatisin.c,v $
  28.  * Revision 2.0  1992/09/13  05:02:44  rosenkra
  29.  * total rewrite. this if first rev of this file.
  30.  *
  31.  *
  32.  */
  33.  
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37.  
  38.  
  39. #include "whatis.h"
  40.  
  41.  
  42. /*
  43.  *    we only need one record here. we read a record and check all the
  44.  *    command line args against it for a match
  45.  */
  46. struct rec    r;
  47.  
  48. char           *libpath = MANPATH;
  49. int        debugging = 0;        /* for -d */
  50.  
  51.  
  52. /*
  53.  *    fcn prototypes
  54.  */
  55. void        whatisin (int, char **);
  56. void        contents (int);
  57. int        parse_record (char *, struct rec *);
  58. void        print_record (int, struct rec *);
  59. void        usage (int);
  60. #ifdef CHECK_MAGIC
  61. int        check_magic (void);
  62. #endif
  63.  
  64.  
  65.  
  66. /*------------------------------*/
  67. /*    main            */
  68. /*------------------------------*/
  69. void main (int argc, char *argv[])
  70. {
  71.     char   *lpath;
  72.  
  73.  
  74.  
  75.     /*
  76.      *   see if there is MANPATH in env. use it over default...
  77.      */
  78.     if ((lpath = getenv ("MANPATH")) != (char *) NULL)
  79.         libpath = lpath;
  80.     else if ((lpath = getenv ("MANDIR")) != (char *) NULL)
  81.         libpath = lpath;
  82. #ifdef DEBUG
  83.     else
  84.         fprintf (stderr, "whatisin: environment variable MANPATH not set, using default\n");
  85. #endif
  86.  
  87.  
  88.     /*
  89.      *   parse args
  90.      */
  91.     for (argc--, argv++; argc && **argv == '-'; argc--, argv++)
  92.     {
  93.         switch (*(*argv+1))
  94.         {
  95.         case 'P':            /* path for db */
  96.         case 'M':
  97.             argc--, argv++;
  98.             if (argc < 1)
  99.                 usage (1);
  100.             libpath = *argv;
  101.             break;
  102.  
  103.         case 'd':            /* debug mode */
  104.             debugging = 1;
  105.             break;
  106.  
  107.         case 'h':            /* help */
  108.             usage (0);
  109.             /* NOTREACHED */
  110.         }
  111.     }
  112.  
  113.  
  114.  
  115.     /*
  116.      *   do it. if no args, print file containing description of sections.
  117.      *   else print contents of sections in argv list
  118.      */
  119.     if (argc < 1)
  120.         whatisin (argc, argv);
  121.     else
  122.         while (argc)
  123.             whatisin (argc--, argv++);
  124.  
  125.     exit (0);
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132. /*------------------------------*/
  133. /*    whatisin        */
  134. /*------------------------------*/
  135. void whatisin (int argc, char **argv)
  136. {
  137.     char        dbname[256];
  138.     char        buf[REC_SIZE];
  139.     int        doall = 0;
  140.     int        sect;
  141.     int        foundit = 0;
  142.  
  143.  
  144.     /*
  145.      *   if no args specified, we only want a description of each section.
  146.      *   otherwise, set sect to that specified in argv. should be single
  147.      *   char (0-9,l,n,o, etc).
  148.      */
  149.     if (argc == 0)
  150.         doall++;
  151.     else
  152.     {
  153.         doall = 0;
  154.         sect = **argv;
  155.     }
  156.  
  157.  
  158.  
  159.     /*
  160.      *   set up name of whatisin database and open it
  161.      */
  162.     sprintf (dbname, "%s%s%s.___", libpath, SLASH, WHATISIN);
  163.     if (freopen (dbname, "r", stdin) == (FILE *) NULL)
  164.     {
  165.         fprintf (stderr,
  166.             "whatisin: could not access file %s\n", dbname);
  167.  
  168.         exit (1);
  169.     }
  170.  
  171.  
  172.  
  173.     if (doall)
  174.     {
  175.         /*
  176.          *   just copy whatisin file. skip lines starting with #
  177.          */
  178.         while (1)
  179.         {
  180.             fgets (buf, REC_SIZE-1, stdin);
  181.             if (feof (stdin))
  182.                 break;
  183.  
  184.             if (buf[0] == '#')
  185.                 continue;
  186.  
  187.             fputs (buf, stdout);
  188.         }
  189.     }
  190.     else
  191.     {
  192.         /*
  193.          *   look for line describing section
  194.          */
  195.         while (1)
  196.         {
  197.             fgets (buf, REC_SIZE-1, stdin);
  198.             if (feof (stdin))
  199.                 break;
  200.  
  201.             if (buf[0] == sect)
  202.             {
  203.                 foundit++;
  204.                 break;
  205.             }
  206.         }
  207.  
  208.  
  209.         if (foundit)
  210.         {
  211.             /*
  212.              *   print header info then get print all the
  213.              *   things in that section. contents will reopen
  214.              *   stdin as the particular whatis database.
  215.              */
  216.             printf ("Section    Description\n");
  217.             printf ("-------    -----------\n");
  218.             printf ("%s\n\n", buf);
  219.             printf ("Contents\n");
  220.             printf ("--------\n");
  221.  
  222.             contents (sect);
  223.  
  224.             printf ("\n");
  225.         }
  226.         else
  227.         {
  228.             fprintf (stderr, "whatisin: no section %c\n", sect);
  229.             exit (1);
  230.         }
  231.     }
  232.     return;
  233. }
  234.  
  235.  
  236.  
  237.  
  238. /*------------------------------*/
  239. /*    contents        */
  240. /*------------------------------*/
  241. void contents (int sect)
  242. {
  243.     char    dbname[256];
  244.     char    buf[REC_SIZE];
  245.  
  246.  
  247.  
  248.     /*
  249.      *   set up db name. section is really a single ascii char, not an
  250.      *   int. this is so we can have whatis for local, new, etc.
  251.      */
  252.     if (sect == -1)
  253.         /* orig behavior (just single "whatis" file, never used here) */
  254.         sprintf (dbname, "%s%s%s", libpath, SLASH, WHATIS);
  255.     else
  256.         /* new: whatis._[0-9lno]_ */
  257.         sprintf (dbname, "%s%s%s._%c_", libpath, SLASH, WHATIS, sect);
  258.  
  259.  
  260.  
  261.     /*
  262.      *   reopen stdin as this file...
  263.      */
  264.     if (debugging)
  265.         fprintf (stderr, "checking database file %s...\n", dbname);
  266.     if (freopen (dbname, "r", stdin) == (FILE *) NULL)
  267.     {
  268.         fprintf (stderr,
  269.             "whatisin: could not access file %s\n", dbname);
  270.  
  271.         exit (1);
  272.     }
  273.  
  274.  
  275.  
  276. #ifdef CHECK_MAGIC
  277.     /*
  278.      *   check file's magic
  279.      */
  280.     if (check_magic ())
  281.     {
  282.         fprintf (stderr,
  283.             "whatisin: magic number is wrong for file %s\n",
  284.             dbname);
  285.  
  286.         exit (1);
  287.     }
  288. #endif
  289.  
  290.  
  291.  
  292.     /*
  293.      *   read file and print lines
  294.      */
  295.     while (1)
  296.     {
  297.         /*
  298.          *   get raw record from file
  299.          */
  300.         fgets (buf, REC_SIZE-1, stdin);
  301.         if (feof (stdin))
  302.             break;
  303.         if (debugging)
  304.             fprintf (stderr, "%s", buf);
  305.  
  306.  
  307.  
  308.         /*
  309.          *   skip comment or blank lines
  310.          */
  311.         if (buf[0] == '#' || buf[0] == '\0' || buf[0] == '\n')
  312.             continue;
  313.  
  314.  
  315.  
  316.         /*
  317.          *   parse the record and store in r
  318.          */
  319.         parse_record (buf, &r);
  320.  
  321.         if (debugging)
  322.         {
  323.         fprintf (stderr, "name:    %s\n", r.name ? r.name : "(NULL)");
  324.         fprintf (stderr, "section: %s\n", r.section ? r.section : "(NULL)");
  325.         fprintf (stderr, "subsect: %s\n", r.subsect ? r.subsect : "(NULL)");
  326.         fprintf (stderr, "desc:    %s\n", r.desc ? r.desc : "(NULL)");
  327.         }
  328.  
  329.  
  330.  
  331.         /*
  332.          *   print record (short form)
  333.          */
  334.         print_record (0, &r);
  335.     }
  336.  
  337.     return;
  338. }
  339.  
  340.  
  341.  
  342.  
  343. /*------------------------------*/
  344. /*    usage            */
  345. /*------------------------------*/
  346. void usage (int excode)
  347. {
  348. #define U(x)    fprintf(stderr,x);
  349.  
  350. U("usage: whatisin [-P path] [section ...]\n");
  351. U("       -P path     alternative path to databases (MANPATH)\n");
  352. U("       section     valid name of a manual section (single char). to get a\n");
  353. U("                   list, invoke whatisin with no args.\n");
  354.  
  355. exit (excode);    
  356. #undef U
  357. }
  358.  
  359.  
  360.